package com.neusoft.utils;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 快递100 查询工具类
 * @author 
 *
 */

public class KD100Service {
	
	/**
	 * 客户授权Key
	 */
	public static final String AUTN_CODE = "boWnjZsy1330";
	
	/**
	 * 实时查询customer
	 */
	public static final String CUSTOMER = "6DC95F7BD79E0305D2710B86234F3D4C";
	
	public static final String URL = "https://poll.kuaidi100.com/poll/query.do";
	
	
	/**
	 * 
	 * @param companyCode 快递公司编号
	 * @param logisticsCode 查询的快递单号
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public  LogisticsResponse queryLogistics(String companyCode,String logisticsCode) throws UnsupportedEncodingException{
		String param = JSON.toJSONString(new QueryParam(companyCode,logisticsCode));
		String sign = MD5Util.MD5Encode(param+AUTN_CODE+CUSTOMER, "utf-8").toUpperCase();
		HashMap<String,Object> params = new HashMap<String,Object>();
		params.put("param",param);
		params.put("sign",sign);
		params.put("customer",CUSTOMER);
		String httpPostRequest = null;
			httpPostRequest = HttpClientUtil.httpPostRequest(URL, params);
			JSONObject parseObject = JSON.parseObject(httpPostRequest);
			System.out.println(parseObject.toJSONString());
			String com = parseObject.getString("com");
			String status = parseObject.getString("status");
			LogisticsResponse javaObject = JSON.toJavaObject(parseObject, LogisticsResponse.class);
			javaObject.setCompanyCode(companyCode);
			javaObject.setLogisticsCode(com);
		return javaObject;
	}
	
	public static void main(String[] args) throws UnsupportedEncodingException {
		LogisticsResponse queryLogistics = new KD100Service().queryLogistics("", "");
		System.out.println(JSON.toJSONString(queryLogistics));
	}
	

}
